﻿using System.Linq;
using System.Web.Mvc;
using Doan.Forum.Common;
using Doan.Forum.Models;
using PagedList;

namespace Doan.Forum.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        private static DoAnForumEntities db = new DoAnForumEntities();

        public ActionResult Index()
        {
            if (Request.IsAuthenticated)
            {
                var crrUser = db.aspnet_Users.SingleOrDefault(x => User.Identity.Name.Equals(x.UserName));
                var check = db.PMessages.Count(x => ((x.ToUserId == crrUser.UserId) && x.Flags));
                if (check > 0)
                {
                    ViewBag.AlertMessage = "Bạn có " + check + " thư chưa đọc";
                }
                else
                {
                    ViewBag.AlertMessage = null;
                }
            }
            else
            {
                ViewBag.AlertMessage = null;
            }
            
            var bv = db.Categories.Where(x => x.Level == 1);
            ViewBag.Category = null;
            return View(bv);
        }

        public ActionResult SubCategoryList(int id)
        {
            var parent = db.Categories.SingleOrDefault(x => x.CategoriId == id);
            ViewBag.ParentName = parent.CategoryName;
            var bv = db.Categories.Where(x => x.ParentId == id);
            ViewBag.Category = null;
            return View(bv);
        }

        public ActionResult TopicList(int id)
        {
            ViewBag.Category = id;
            var currcate = db.Categories.SingleOrDefault(x => x.CategoriId == id);
            var parentcate = db.Categories.SingleOrDefault(x => x.CategoriId == currcate.ParentId);
            string location = parentcate.CategoryName + " >> " + currcate.CategoryName;
            ViewBag.location = location;
            return View();
        }

        public ActionResult TopicListPartial(int id, int? page)
        {
            int pageNumber = (page ?? 1);
            var bv = db.Topics.Where(x => x.CategoriId == id).OrderBy(x => x.TopicCreationDate).ToPagedList(pageNumber, Constant.PageSize);
            var currcate = db.Categories.SingleOrDefault(x => x.CategoriId == id);
            var parentcate = db.Categories.SingleOrDefault(x => x.CategoriId == currcate.ParentId);
            string location = parentcate.CategoryName + " >> " + currcate.CategoryName;
            ViewBag.location = location;
            ViewBag.pageNumber = pageNumber;
            return PartialView("TopicListPartial",bv);
        }

        [HttpPost]
        public ActionResult Search(string keyword)
        {
            ViewBag.Keyword = keyword;
            return View();
        }

        public ActionResult SearchPartial(string id, int? page)
        {
            int pageNumber = (page ?? 1);
            var bv =
                db.Topics.Where(
                    x => x.TopicTitle.Contains(id) || x.Comments.FirstOrDefault().CommentBody.Contains(id)).OrderBy(x => x.TopicCreationDate).ToPagedList(pageNumber, Constant.PageSize);
            ViewBag.pageNumber = pageNumber;
            return PartialView("SearchPartial",bv);
        }

        public ActionResult SearchbyTag(int id)
        {
            ViewBag.TagId = id;
            return View();
        }

        public ActionResult SearchbyTagPartial(int id, int? page)
        {
            int pageNumber = (page ?? 1);
            var tag = db.Tags.SingleOrDefault(x => x.Id == id);
            var bv = tag.Topics.OrderBy(x => x.TopicCreationDate).ToPagedList(pageNumber, Constant.PageSize);
            ViewBag.pageNumber = pageNumber;
            return PartialView("SearchbyTagPartial",bv);
        }
    }
}
